using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodeContrib.Web.WebControls
{
	[ToolboxData("<{0}:FormPanel runat=\"server\"></{0}:FormPanel>")]
	public class FormPanel : WebControl, INamingContainer
	{
		public string DefaultButton { get; set; }

		private string GetDefaultButtonPostBackScript(string buttonUniqueId)
		{
			return "javascript:if(event && (event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) { _doPostBack(\"" + buttonUniqueId + "\" , \"\"); return false; } else { return true; }";
		}

		private string GetFocusElementScript(string uniqueId)
		{
			return "javascript:document.getElementById(\"" + uniqueId + "\").focus();";
		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write(string.Format("<div onkeypress=\"{0}\">", GetDefaultButtonPostBackScript(DefaultButton)));

			RenderChildren(writer);

			writer.Write("</div>");
		}
	}
}